package com.enterprise.viewer.client;

import com.enterprise.viewer.client.event.ServiceEvent;
import com.enterprise.viewer.client.event.ServiceEvent.CATEGORY;
import com.enterprise.viewer.client.event.ServiceEvent.EVENTTYPE;
import com.enterprise.viewer.client.event.ServiceEvent.NAME;
import com.enterprise.viewer.client.event.ServiceEvent._Event;
import com.enterprise.viewer.shared.contentproviders.ContentProviders;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;


public class EnterpriseViewerServiceManager extends ServiceManager {

	/**
	 * The async counterpart of <code>DeityService</code>.
	 */
	public interface EnterpriseViewerServiceAsync {
		void getContentProviders(AsyncCallback<ContentProviders> callback)
				throws IllegalArgumentException;
		void getServices(AsyncCallback<ContentProviders> callback)
				throws IllegalArgumentException;

	}

	/**
	 * The client side stub for the RPC service.
	 */
	@RemoteServiceRelativePath("service")
	public interface EnterpriseViewerService extends RemoteService {
		ContentProviders getContentProviders() throws IllegalArgumentException;
		ContentProviders getServices() throws IllegalArgumentException;

	}

	private final EnterpriseViewerServiceAsync service = GWT.create(EnterpriseViewerService.class);

	public EnterpriseViewerServiceManager(SimpleEventBus eventBus) {
		super(eventBus);
		init();
	}

	private void init(){
		eventBus.addHandler(ServiceEvent.TYPE, new ServiceEvent._EventHandler() {

			@Override
			public void onChange(_Event event) {
				if(event.getType().compareTo(EVENTTYPE.RESPONSE) == 0 || event.getType().compareTo(EVENTTYPE.BROADCAST) == 0){
					if(event.getCategory().compareTo(CATEGORY.GET) == 0){
						if(event.getName().compareTo(NAME.CONTENTPROVIDERS) == 0){
						}
					}
				}
				else if(event.getType().compareTo(EVENTTYPE.REQUEST) == 0){
					if(event.getCategory().compareTo(CATEGORY.GET) == 0){
						if(event.getName().compareTo(NAME.CONTENTPROVIDERS) == 0){
							handleGetContentProviders();
						}
						else if(event.getName().compareTo(NAME.SERVICES) == 0){
							handleGetServices();
						}
					}
				}
			}});
	}
	
	private void handleGetContentProviders(){
		service.getContentProviders(new AsyncCallback<ContentProviders>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("service.getContentProviders failed");										
			}

			@Override
			public void onSuccess(ContentProviders result) {
				Object[] params = new Object[1];
				params[0] = result;
				ServiceEvent.fireEvent(eventBus, EVENTTYPE.RESPONSE, CATEGORY.GET, NAME.CONTENTPROVIDERS, params);										
			}
		});
	}
	
	private void handleGetServices(){
		service.getServices(new AsyncCallback<ContentProviders>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("service.getServices failed");										
			}

			@Override
			public void onSuccess(ContentProviders result) {
				Object[] params = new Object[1];
				params[0] = result;
				ServiceEvent.fireEvent(eventBus, EVENTTYPE.RESPONSE, CATEGORY.GET, NAME.SERVICES, params);										
			}
		});
	}
}
